import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Random;

import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Component;
import javax.swing.Box;
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTable;
import javax.swing.BoxLayout;


/**
 * This class represents the main GUI that we will create in the game. This GUI houses all the in game components
 * like the ShortRangeChart, Shipyard, Marketplace, and SolarSystem information.
 * @author Araya Zaesim, Stanley Ayzenberg, Michelle Partogi, Adam Perez
 * @version 1.0
*/

public class SpaceTraderGui {
	
	private JFrame frame;
	private Player player;
	private ArrayList<SolarSystem> universe;
	private JLabel lblPlayername;
	private JPanel mainPanel;
	private ShortRangeChart chartPanel;
	private JPanel northPanel;
	private JPanel topMenuPanel;
	private JButton btnNewGame;
	private JButton btnSaveGame;
	private JButton btnLoadGame;
	private JPanel tabPanel_1;
	private JPanel tabPanel_2;
	private JPanel infoPanel;
	private JLabel lblSolarSystem;
	private JLabel lblCredits;
	private JTabbedPane tabbedPane;
	private JPanel curSSPanel_1;
	private JPanel systemPanel;

	private Random rand;

	private SolarSystem currentSystem = null;
	private SolarSystem targetSystem = null;
	private MarketPlaceLogic mpLogic;
	private int[] buyPrices = new int[10];
	private int[] sellPrices = new int[10];
	private JLabel label_5;
	private JLabel Curr;
	private JLabel label_1;
	private JLabel label_2;
	

	private JPanel gamePanel;
	private JLabel bg;
	private SystemPanel tarSystemInfo;
	private SystemPanel currSystemInfo;
	private JButton btnWarp;
	private JLabel lblFuelLevel;
	private Marketplace marketplace;
	private SaveLoad saveload;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SpaceTraderGui window = new SpaceTraderGui();					
					window.frame.setVisible(true);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 * Also creates the universe with allSystems.
	 */
	public SpaceTraderGui() {
		initialize();
	}
	public ShortRangeChart getChartPanel() {
		return chartPanel;
	}


	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		rand = new Random();
		frame = new JFrame();
		//frame.setResizable(false);
		frame.setPreferredSize(new Dimension(1300, 1200) );
		frame.setBounds(1200, 100, 950, 650);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new BorderLayout(0, 0));
		
		mainPanel = new JPanel();
		frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
		mainPanel.setLayout(new BorderLayout(0, 0));
		
		northPanel = new JPanel();
		mainPanel.add(northPanel, BorderLayout.NORTH);
		northPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		northPanel.setBackground(new Color(3,2,4));
		
		topMenuPanel = new JPanel();
		northPanel.add(topMenuPanel);
		topMenuPanel.setBackground(new Color(3,2,4));
		
		saveload = new SaveLoad(this);
		
		btnNewGame = new JButton("New Game");
		btnNewGame.addActionListener(new ActionListener() {
			
			/*
			 * Creates a config window when New Game is pressed so that a user can enter skill points, name, and choose a
			 * difficulty.
			 */
			public void actionPerformed(ActionEvent e) {
				ConfigWindow window = new ConfigWindow(getGui());						
			}
		});
		topMenuPanel.add(btnNewGame);
		
		btnSaveGame = new JButton("Save Game");
		btnSaveGame.addActionListener(new ActionListener() {
			/*
			 * Saves the game
			 */
			public void actionPerformed(ActionEvent e) {
				saveload.saveGame();
			}
		});
		topMenuPanel.add(btnSaveGame);
		
		btnLoadGame = new JButton("Load Game");
		btnLoadGame.addActionListener(new ActionListener() {
			/*
			 * Loads an existing game
			 */
			public void actionPerformed(ActionEvent e) {
				saveload.loadGame();
			}
		});
		topMenuPanel.add(btnLoadGame);
		
		gamePanel = new JPanel();
		mainPanel.add(gamePanel, BorderLayout.CENTER);
		gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.Y_AXIS));
		
		
		bg = new JLabel(new ImageIcon("intro.jpg"));
		bg.setPreferredSize(new Dimension(900,900));
		gamePanel.add(bg);
		
	}
	
	public void startGame(Player newPlayer){
		rand = new Random();
		if (universe == null)
			gamePanel.remove(bg);
		else{
			mainPanel.remove(gamePanel);
			gamePanel = new JPanel();
			mainPanel.add(gamePanel, BorderLayout.CENTER);
			gamePanel.setLayout(new BoxLayout(gamePanel, BoxLayout.Y_AXIS));
			currentSystem =null;
			targetSystem = null;
			
		}
		Universe un = new Universe();
		this.universe = un.getSystems();
		
		infoPanel = new JPanel();
		gamePanel.add(infoPanel);
		
		lblSolarSystem = new JLabel("Solar System: ");
		infoPanel.add(lblSolarSystem);
		
		Component horizontalStrut = Box.createHorizontalStrut(60);
		infoPanel.add(horizontalStrut);
		lblPlayername = new JLabel("Player: ");
		infoPanel.add(lblPlayername);
		lblPlayername.setHorizontalAlignment(SwingConstants.RIGHT);
		
		Component horizontalStrut_1 = Box.createHorizontalStrut(60);
		infoPanel.add(horizontalStrut_1);
		
		lblCredits = new JLabel("Credits: ");
		infoPanel.add(lblCredits);
		
		tabPanel_1 = new JPanel();
		tabPanel_2 = new JPanel();
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		gamePanel.add(tabbedPane);
		tabbedPane.addTab("Marketplace", null, tabPanel_1, null);
		tabbedPane.addTab("Shipyard", null, tabPanel_2, null);
		
		JPanel shipYardPanel = new JPanel();
		tabPanel_2.add(shipYardPanel);
		tabPanel_1.setLayout(new BoxLayout(tabPanel_1, BoxLayout.Y_AXIS));
		
		curSSPanel_1 = new JPanel();
		curSSPanel_1.setPreferredSize(new Dimension(600, 350));
		tabPanel_1.add(curSSPanel_1);
		curSSPanel_1.setLayout(new BoxLayout(curSSPanel_1, BoxLayout.X_AXIS));
		
		label_1 = new JLabel("");
		curSSPanel_1.add(label_1);
		
		label_2 = new JLabel("");
		curSSPanel_1.add(label_2);
		/**
		 * Makes system Panel!!!
		 */
		
		systemPanel = new JPanel();
		tabPanel_1.add(systemPanel);
		systemPanel.setLayout(new GridLayout(1, 3, 0, 0));
		
		currSystemInfo = new SystemPanel("Current");
		systemPanel.add(currSystemInfo);
		
		tarSystemInfo = new SystemPanel("Target");
		systemPanel.add(tarSystemInfo);
		System.out.println(universe);
		if (currentSystem == null) {
			currentSystem = universe.get(rand.nextInt(universe.size()));
			currSystemInfo.update(currentSystem);
			lblSolarSystem.setText("System: " + currentSystem.getName());
		}
		JPanel holder = new JPanel();
		systemPanel.add(holder);
		JPanel fuelWarp = new JPanel();
		fuelWarp.setLayout(new BoxLayout(fuelWarp, BoxLayout.Y_AXIS));
		
		btnWarp = new JButton("Warp");
		btnWarp.setEnabled(false);
		fuelWarp.add(btnWarp);
		
		lblFuelLevel = new JLabel("Fuel Level: ");
		fuelWarp.add(lblFuelLevel);
		systemPanel.add(fuelWarp);
		
		setPlayer(newPlayer);
		/**
		 * Moves to target system
		 */
		btnWarp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				player.getShip().updateFuelFromTravel(currentSystem, targetSystem, player);
				currentSystem = targetSystem;//need to go after fuel update
				marketplace.newMarketPlace(currentSystem); //needs to go after system swap
				currSystemInfo.update(targetSystem);
				currSystemInfo.setEvent(marketplace.getEvent());
				tarSystemInfo.clear();
				lblFuelLevel.setText("Fuel Level: " + player.getShip().getFuel() + "/" + player.getShip().getMaxFuel());
				targetSystem = null;
				btnWarp.setEnabled(false);
				lblSolarSystem.setText("System: " + currentSystem.getName());
				chartPanel.setCurrSystem(currentSystem);
				chartPanel.updateRange();
				chartPanel.repaint();
				System.out.println("btnWarp");
			}
		});//end action listener
	}
	/**
	 * Sets the data of the player within the GUI by getting the player data of the object created by users.
	 * @param player The player object for the GUI
	 */
	public void setPlayer(Player player) {
		this.player = player;
		lblPlayername.setText("Player: " + player.getName());
		lblCredits.setText("Credits: "+player.getCredits());
		createMarketPlace(player);
	}
	
	/**
	 * Updates the target system information as the user clicks different systems.
	 * @param system
	 */
	
	
	public void createMarketPlace(Player player){
		if(marketplace != null) curSSPanel_1.remove(marketplace);
		marketplace = new Marketplace(player, currentSystem, this);
		curSSPanel_1.add(marketplace);
		makeShortRangeChart();
	}
	
	
	public void makeShortRangeChart() {
		if(chartPanel != null) curSSPanel_1.remove(chartPanel);
		chartPanel = new ShortRangeChart(universe, this, player);
		lblFuelLevel.setText("Fuel Level: " + player.getShip().getFuel() + "/" + player.getShip().getMaxFuel());
		chartPanel.setLayout(new GridLayout(2, 0, 4, 0));
		chartPanel.setCurrSystem(currentSystem);
		curSSPanel_1.add(chartPanel, BorderLayout.EAST);
		currSystemInfo.setEvent(marketplace.getEvent());
	}
	
	/**
	 * Gets data of the player
	 * @return The player object
	 */
	public Player getPlayer() {
		return player;
	}
	
	public void setCredits(int credits){
		lblCredits.setText("Credits: "+player.getCredits());
	}
	
	

	public void updateTargetSystem(SolarSystem system) {
		targetSystem = system;
		//btnWarp.setEnabled(true);
		tarSystemInfo.update(currentSystem, targetSystem);
		chartPanel.setTarSystem(targetSystem);
		if(chartPanel.inRange(currentSystem, system)) {
			btnWarp.setEnabled(true);
		} else btnWarp.setEnabled(false);
	
	}
	public void updateSolarSystem() {
		lblSolarSystem.setText("System: " + currentSystem.getName());
		chartPanel.setCurrSystem(currentSystem);
		System.out.println("updateSolarSystem");
		String event;
		if (rand.nextInt(100)<10){
			int index = rand.nextInt(7);
			String[] array = {"Lack of Workers","Boredom","Plague","War","Cropfail","Cold","Drought"};
			event = array[index];
		}
		else{
			event = "None";
		}
		mpLogic = new MarketPlaceLogic(currentSystem, event);
		buyPrices = mpLogic.buyPrices();
		sellPrices = mpLogic.sellPrices();
		
	}
	
	/**
	 * A getter for the GUI itself (and all of the data within it)
	 * @return The GUI
	 */
	private SpaceTraderGui getGui() {
		return this;
	}
}
